package com.medranocg.gd



import static org.springframework.http.HttpStatus.*
import grails.gorm.DetachedCriteria
import grails.transaction.Transactional
import com.medranocg.gd.crecerxxi.CrecerXXI

@Transactional(readOnly = true)
class BuscarDocumentoController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index() {
    }

	def buscar() {
		def listDocument = Document.findAll()
				
		def criteria = new DetachedCriteria(Document).build {
			eq "lastVersion", true
			isNull "deleteDate"
		}

		if (params.customerId) {
			def customer = Customer.findByIdPersona(params.customerId as Long)
			criteria = criteria.build {
				eq "customer", customer
			}
		}
		
		if (params.cajaId) {
			def box = Box.findByCajaId(params.cajaId)

			criteria = criteria.build {
				eq "box", box
			}
		}
		
		def result = criteria.list(offset: params.offset ?: 0, max: 2)
		
		render(view: 'index', model: [listDocument: result, documentInstanceCount: criteria.count()])
	}

    def show(Document documentInstance) {
        respond documentInstance
    }
	
    def create() {
        respond new Document(params)
    }

    @Transactional
    def save(Document documentInstance) {
        if (documentInstance == null) {
            notFound()
            return
        }

        if (documentInstance.hasErrors()) {
            respond documentInstance.errors, view:'create'
            return
        }

        documentInstance.save flush:true

        request.withFormat {
            form {
                flash.message = message(code: 'default.created.message', args: [message(code: 'documentInstance.label', default: 'Document'), documentInstance.id])
                redirect documentInstance
            }
            '*' { respond documentInstance, [status: CREATED] }
        }
    }

    def edit(Document documentInstance) {
        respond documentInstance
    }

    @Transactional
    def update(Document documentInstance) {
        if (documentInstance == null) {
            notFound()
            return
        }

        if (documentInstance.hasErrors()) {
            respond documentInstance.errors, view:'edit'
            return
        }

        documentInstance.save flush:true

        request.withFormat {
            form {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'Document.label', default: 'Document'), documentInstance.id])
                redirect documentInstance
            }
            '*'{ respond documentInstance, [status: OK] }
        }
    }

    @Transactional
    def delete(Document documentInstance) {

        if (documentInstance == null) {
            notFound()
            return
        }

        documentInstance.delete flush:true

        request.withFormat {
            form {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'Document.label', default: 'Document'), documentInstance.id])
                redirect action:"modalIndex", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'documentInstance.label', default: 'Document'), params.id])
                redirect action: "modalIndex", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
